package health_system.health_system.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import health_system.health_system.entity.Moment;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MomentDTO {
    private Long id;
    private Long userId;
    private String username;
    private String userAvatarUrl;
    private String content;
    private List<MomentMediaDTO> mediaList;
    private Integer likeCount;
    private Integer commentCount;
    private Integer shareCount;
    private Moment.VisibilityType visibility;
    private Integer visibilityDuration;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    private Boolean isLikedByCurrentUser;
    private Boolean isSharedByCurrentUser;
    
    public static MomentDTO fromEntity(Moment moment) {
        if (moment == null) {
            return null;
        }
        
        MomentDTO dto = new MomentDTO();
        dto.setId(moment.getId());
        
        if (moment.getUser() != null) {
            dto.setUserId(moment.getUser().getId());
            dto.setUsername(moment.getUser().getUsername());
            dto.setUserAvatarUrl(moment.getUser().getAvatarUrl());
        }
        
        dto.setContent(moment.getContent());
        dto.setLikeCount(moment.getLikes() != null ? moment.getLikes().size() : 0);
        dto.setCommentCount(moment.getComments() != null ? moment.getComments().size() : 0);
        dto.setShareCount(moment.getShares() != null ? moment.getShares().size() : 0);
        dto.setVisibility(moment.getVisibility());
        dto.setVisibilityDuration(moment.getVisibilityDuration());
        dto.setCreatedAt(moment.getCreatedAt());
        dto.setUpdatedAt(moment.getUpdatedAt());
        
        if (moment.getMediaList() != null && !moment.getMediaList().isEmpty()) {
            dto.setMediaList(moment.getMediaList().stream()
                    .map(MomentMediaDTO::fromEntity)
                    .collect(Collectors.toList()));
        }
        
        return dto;
    }
    
    public static MomentDTO fromEntityWithLikeStatus(Moment moment, Long currentUserId) {
        MomentDTO dto = fromEntity(moment);
        if (dto != null && moment.getLikes() != null) {
            dto.setIsLikedByCurrentUser(moment.getLikes().stream()
                    .anyMatch(like -> like.getUser().getId().equals(currentUserId)));
        }
        return dto;
    }
    
    public static MomentDTO fromEntityWithShareStatus(Moment moment, Long currentUserId) {
        MomentDTO dto = fromEntity(moment);
        if (dto != null && moment.getShares() != null) {
            boolean isShared = moment.getShares().stream()
                    .anyMatch(share -> share.getUser().getId().equals(currentUserId));
            dto.setIsSharedByCurrentUser(isShared);
        }
        return dto;
    }
} 